package br.ufrn.imd.classificados.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import br.ufrn.imd.classificados.tasks.ApiGetItemsTask;
import br.ufrn.imd.imd0023_classificados.R;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.SubMenu;

/**
 * Tela com a lista de anúncios.
 * @author Pablo
 *
 */
public class ListaAnunciosActivity extends SherlockActivity implements OnItemClickListener {

	private ListView listViewSelling;
	private ListView listViewBuying;

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add("Novo Anúncio").setIcon(R.drawable.ic_action_new).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
		
		SubMenu subMenu1 = menu.addSubMenu("Menu");
        subMenu1.add("Oportunidades de Compra");
        subMenu1.add("Histórico");
        subMenu1.add("Editar Perfil");

        MenuItem subMenu1Item = subMenu1.getItem();
        subMenu1Item.setIcon(R.drawable.ic_action_overflow);
        subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
        
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getTitle().equals("Novo Anúncio")) {
			Intent intentNovoAnuncio = new Intent(this, NovoAnuncioActivity.class);
			startActivity(intentNovoAnuncio);
		} else if(item.getTitle().equals("Oportunidades de Compra")) {
			Intent intentOportunidades = new Intent(this, OportunidadesCompraActivity.class);
			startActivity(intentOportunidades);
		} else if(item.getTitle().equals("Histórico")) {
			Intent intentHistorico = new Intent(this, HistoricoTransacoesActivity.class);
			startActivity(intentHistorico);
		} else if(item.getTitle().equals("Editar Perfil")) {
			Intent intentEditarPerfil = new Intent(this, EditarPerfilActivity.class);
			startActivity(intentEditarPerfil);
		}
		return true;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.item_listview);

		listViewSelling = (ListView) findViewById(R.id.itens_1);
		listViewSelling.setOnItemClickListener(this);

		listViewBuying = (ListView) findViewById(R.id.itens_2);
		listViewBuying.setOnItemClickListener(this);
	}

	@Override
	public void onResume() {
		super.onResume();
		SharedPreferences settings = getSharedPreferences("UserProfile", MODE_PRIVATE);
		new ApiGetItemsTask("items", this).execute("?user_email=" + settings.getString("email", ""));
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Intent intentEditarAnuncio = new Intent(this, EditarAnuncioActivity.class);
		startActivity(intentEditarAnuncio);
	}
}
